package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;

public class SelectActivity extends BaseActivity {
    private RecyclerViewCustom dataBinding;

    private ArrayList<SelectBean> returnSelectBeans = new ArrayList<>();
    private int selectMaxNum;
    private ArrayList<SelectBean> selectedBeans = new ArrayList<>();
    private SlimAdapterEx slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "请选择");
        initTopRightText("提交", v -> {

            finishAndResult();
        });
    }

    private void finishAndResult() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(Config.SELECTED_BEANS, returnSelectBeans);
        setResult(Config.SUCCESS, intent);
        finish();
    }

    @Override
    public void initData() {
        selectMaxNum = getIntent().getIntExtra(Config.MAX_NUM, Integer.MAX_VALUE);
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_07, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector) {
                injector.text(R.id.tv_content, data);
            }
        })
                .register(R.layout.list_item_04, new SlimInjector<SelectBean>() {
                    @Override
                    public void onInject(SelectBean data, IViewInjector injector) {
                        injector.text(R.id.tv_content, data.getContent())
                                .imageCircle(R.id.iv_head, data.getHeadImg())
                                .checked(R.id.checkbox, data.isChecked());
                    }
                }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {

                    if (slimAdapterEx.getData().get(position) instanceof String) {
                        switch (position) {
                            case 0:
                                startActivityForResult(SelectTongshiActivity.class, null, Config.SELECT_TONGSHI);
                                break;
                            case 1:
                                startActivityForResult(SelectFriendActivity.class, null, Config.SELECT_FRIEND);
                                break;
                            case 2:
                                startActivityForResult(SelectQunzuActivity.class, null, Config.SELECT_QUN_MEMBER);
                                break;
                        }
                    } else {
                        SelectBean bean = (SelectBean) slimAdapterEx.getDataItem(position);
                        bean.setChecked(!bean.isChecked());
                        if (bean.isChecked()) {
                            returnSelectBeans.add(bean);
                        } else {
                            returnSelectBeans.remove(bean);
                        }
                        slimAdapterEx.notifyItemChanged(position);

                    }
                });
    }

    private void refresh() {
        slimAdapterEx.getData().clear();
        slimAdapterEx.getData().add("同事");
        slimAdapterEx.getData().add("好友");
        slimAdapterEx.getData().add("群成员");
        RongIM.getInstance().getConversationList(new RongIMClient.ResultCallback<List<Conversation>>() {
            @Override
            public void onSuccess(List<Conversation> conversations) {
                System.out.println(Thread.currentThread().getName());
                if (conversations != null) {
                    for (Conversation conversation : conversations) {
                        String portrait = null;
                        String title;
                        title = conversation.getConversationTitle();
                        if (TextUtils.isEmpty(title)) {
                            title = RongContext.getInstance().getConversationTemplate(conversation.getConversationType().getName()).getTitle(conversation.getTargetId());
                        }

                        portrait = conversation.getPortraitUrl();
                        if (TextUtils.isEmpty(portrait)) {
                            Uri groupUserInfo = RongContext.getInstance().getConversationTemplate(conversation.getConversationType().getName()).getPortraitUri(conversation.getTargetId());
                            portrait = groupUserInfo != null ? groupUserInfo.toString() : null;
                        }
                        SelectBean selectBean = new SelectBean();
                        selectBean.setHeadImg(portrait);
                        selectBean.setContent(title);
                        selectBean.setGuid(conversation.getTargetId());
                        selectBean.setType(conversation.getConversationType());
                        slimAdapterEx.getData().add(selectBean);
                    }
                }
                slimAdapterEx.notifyItemRangeChanged(0, slimAdapterEx.getData().size());

            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                slimAdapterEx.notifyItemRangeChanged(0, slimAdapterEx.getData().size());

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_FRIEND:
                    selectedBeans.clear();
                    selectedBeans = data.getParcelableArrayListExtra(Config.SELECTED_BEANS);
                    finishAndResult();
                    break;

            }
        }
    }

    @Override
    public void setView() {
        refresh();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }


}
